package com.hongyi.serivice.impl;

import com.hongyi.mapper.UserMapper;
import com.hongyi.pojo.User;
import com.hongyi.serivice.UserService;
import com.hongyi.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: hongyi
 * @CreateTime: 2024-10-22
 */


@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserVo> findALl() {
        List<UserVo> userVos = new ArrayList<>();

        List<User> list = userMapper.findALl();
        if (list != null && list.size() > 0) {

            for (int i = 0; i < list.size(); i++) {
                User user = list.get(i);
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(user, userVo);
                userVos.add(userVo);
            }

        }
        return userVos;
    }

    @Override
    public UserVo getById(Long id) {
        User user = userMapper.getById(id);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }

    @Override
    public void save(User user) {
        userMapper.save(user);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public void delete(Long id) {
        userMapper.delete(id);
    }

    @Override
    public List<UserVo> findName(String name) {
        List<User> userList = userMapper.findName(name);
        List<UserVo> list = new ArrayList<>();
        if (userList != null && userList.size() > 0) {

            for (int i = 0; i < userList.size(); i++) {
                User user = userList.get(i);
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(user, userVo);
                list.add(userVo);
            }
        }
        return list;
    }

    @Override
    public List<UserVo> findUser(User user) {
        List<User> userList = userMapper.findUser(user);
        List<UserVo> list = new ArrayList<>();
        if (userList != null && userList.size() > 0) {

            for (int i = 0; i < userList.size(); i++) {
                User u = userList.get(i);
                UserVo userVo = new UserVo();
                BeanUtils.copyProperties(u, userVo);
                list.add(userVo);
            }
        }
        return list;
    }
}
